package cn.ava.basic.controller;

import cn.ava.basic.dto.EmailRegisterDto;
import cn.ava.basic.dto.SmsRegisterDto;
import cn.ava.basic.exception.BusinessException;
import cn.ava.basic.service.IVerifyCodeService;
import cn.ava.basic.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName: VerifyCodeController
 * @Description: 随机验证码控制层
 * @Author: Avavaava
 * @Date: 2022/7/5 15:22
 * @version: 23.53
 *
 */
@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {

    @Autowired
    private IVerifyCodeService verifyCodeService;


    /**
     * @Title: graph
     * @Description: 获取一个随机图形验证码
     * @Author: Avavaava
     * @Version: 23.53
     * @Date: 2022/7/5 15:23
     * @param:
     * @return:
     */
    @GetMapping("/image/{imageCodeKey}")
    public AjaxResult graph(@PathVariable("imageCodeKey") String imageCodeKey) {
        try {
            String base64Str = verifyCodeService.getGraph(imageCodeKey);
            return new AjaxResult().setResultObj(base64Str);
        } catch (BusinessException e){
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

    /**
     * @Title: smsCode
     * @Description: 发送短信验证码
     * @Author: Avavaava
     * @Version: 23.53
     * @Date: 2022/7/26 16:14
     * @param:
     * @return:
     */
    @PostMapping("/smsCode")
    public AjaxResult smsCode(@RequestBody SmsRegisterDto smsRegisterDto) {
        try {
            verifyCodeService.smsCode(smsRegisterDto);
            return new AjaxResult();
        } catch (BusinessException e){
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

    /**
     * @Title: emailCode
     * @Description: 邮箱验证码
     * @Author: Avavaava
     * @Version: 23.53
     * @Date: 2022/7/26 16:14
     * @param:
     * @return:
     */
    @PostMapping("/emailCode")
    public AjaxResult emailCode(@RequestBody EmailRegisterDto emailRegisterDto) {
        try {
            verifyCodeService.sendMailCode(emailRegisterDto);
            return new AjaxResult();
        } catch (BusinessException e){
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }


}
